package com.example.myproject.controller;

import com.example.myproject.dto.exchange.ExchangeRequest;
import com.example.myproject.dto.exchange.ExchangeResponse;
import com.example.myproject.dto.exchange.TitleInfoDTO;
import com.example.myproject.security.JwtAuthenticationFilter;
import com.example.myproject.service.ExchangeService;
import com.example.myproject.vo.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ExchangeController {

    @Autowired
    private ExchangeService exchangeService;

    @PostMapping("/points/exchange/score")
    public ApiResponse<ExchangeResponse> exchangeTicketToMagic(@RequestBody ExchangeRequest request) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        Long userId = ((JwtAuthenticationFilter.UserPrincipal) auth.getPrincipal()).getId();

        ExchangeResponse response = exchangeService.exchangeTicketToMagic(userId, request);
        return ApiResponse.success(200, "兑换成功", response);
    }

    @PostMapping("/points/exchange/point")
    public ApiResponse<ExchangeResponse> exchangeTicketToPoint(@RequestBody ExchangeRequest request) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        Long userId = ((JwtAuthenticationFilter.UserPrincipal) auth.getPrincipal()).getId();

        ExchangeResponse response = exchangeService.exchangeTicketToPoint(userId, request);
        return ApiResponse.success(200, "兑换成功", response);
    }

    @PostMapping("/magic/exchange/title")
    public ApiResponse<ExchangeResponse> exchangeMagicToTitle(@RequestParam Long titleId) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        Long userId = ((JwtAuthenticationFilter.UserPrincipal) auth.getPrincipal()).getId();

        ExchangeResponse response = exchangeService.exchangeMagicToTitle(userId, titleId);
        return ApiResponse.success(200, "兑换成功", response);
    }

    @GetMapping("/titles/user")
    public ApiResponse<List<TitleInfoDTO>> getUserTitles() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        Long userId = ((JwtAuthenticationFilter.UserPrincipal) auth.getPrincipal()).getId();

        List<TitleInfoDTO> titles = exchangeService.getUserTitles(userId);
        return ApiResponse.success(200, "获取成功", titles);
    }

    @GetMapping("/titles/all")
    public ApiResponse<List<TitleInfoDTO>> getAllAvailableTitles() {
        List<TitleInfoDTO> titles = exchangeService.getAllAvailableTitles();
        return ApiResponse.success(200, "获取成功", titles);
    }

    // 新增：点券充值接口
    @PostMapping("/ticket/recharge")
    public ApiResponse<ExchangeResponse> rechargeTicket(@RequestBody ExchangeRequest request) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        Long userId = ((JwtAuthenticationFilter.UserPrincipal) auth.getPrincipal()).getId();

        ExchangeResponse response = exchangeService.rechargeTicket(userId, request);
        return ApiResponse.success(200, "充值成功", response);
    }
}